Module# 09: Table and Map                                          Lecture#39: Applications of Map Part-II

 

// Example 39.1: Creating a Map container as a TreeMap class object

 

// Illustrates the creation of a map container

import java.util.*;

class TreeMapDemo {

     public static void main(String args[]) {

      // Create a tree map.

      TreeMap<String, Double> tm = new TreeMap<String, Double>();

      // Put elements to the map.

      tm.put("John Doe", new Double(3434.34));

      tm.put("Tom Smith", new Double(123.22));

      tm.put("Jane Baker", new Double(1378.00));

      tm.put("Tod Hall", new Double(99.22));

      tm.put("Ralph Smith", new Double(-19.08));

      // Get a set of the entries.

      Set<Map.Entry<String, Double>> set = tm.entrySet();

      // Display the elements.

      for(Map.Entry<String, Double> me : set) {

             System.out.print(me.getKey() + ": ");

         System.out.println(me.getValue());

   }

 

System.out.println();

      // Deposit 1000 into John Doe's account.

      double balance = tm.get("John Doe");

      tm.put("John Doe", balance + 1000);

      System.out.println("John Doe's new balance: " + tm.get("John Doe"));

    }

}

 

// Example 39.2: Managing a Map container as a LinkedHashMap class object

 

/* This program illustrates the creation of a Map object using LinkedHashMap class */

import java.util.*;

 

class LinkedHashMapDemo {

     private static final int MAX = 6; // Refers to the max size of the map

 

     public static void main(String args[]) {

      // Creating the linked hashmap and implementing removeEldestEntry() to MAX size

      LinkedHashMap<Integer, String>lhm = 

      new LinkedHashMap<Integer, String>() {

           protected Boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {

            return size() > MAX;

      }

      };

 

      // Adding elements using put()

      lhm.put(0, "Welcome");

      lhm.put(1, "To");

      lhm.put(2, "The");

      lhm.put(3, "World");

      lhm.put(4, "Of");

      lhm.put(5, "Java");

 

      System.out.println("" + lhm);

 

      // Adding more elements

      lhm.put(6, "Joy with Java");

 

      // Displying the map after adding one more element

      System.out.println("" + lhm);

 

      // Adding more elements

      lhm.put(7, "Hello");

 

      // Displying the map after adding one more element

      System.out.println("" + lhm);

     }

}

 

 

// Example 39.3: Map container with user defined data type

 

/* This example illustrates how a map container will be with objects of Book class. */

 

import java.util.*;

 

class Book {   

  int id;   

  String name,author,publisher;   

  int quantity;   

 

  public Book(int id, String n, String a, String p, int q){   

    this.id = id;   

    name = n;   

    author = a;   

    publisher = p;   

    quantity = q;   

  }   

}

/* This example illustrates how a map container will be with objects of Book class. */

 

import java.util.*;

 

class Book {   

  int id;   

  String name,author,publisher;   

  int quantity;   

 

  public Book(int id, String n, String a, String p, int q){   

    this.id = id;   

    name = n;   

    author = a;   

    publisher = p;   

    quantity = q;   

  }   

}

 

public class MapBookDataExample {   

     public static void main(String[] args) {   

      //Creating map of Books   

      Map<Integer,Book> map=new LinkedHashMap<Integer,Book>();   

      //Creating Books   

      Book b1=new Book(101,"Python","Ponting","Oxford",8);   

      Book b2=new Book(102,"Java","Spielberg","McGraw Hill",4);   

      Book b3=new Book(103,"C++","Galvin","Wiley",6);   

      //Adding Books to map  

      map.put(2,b2); 

      map.put(1,b1); 

      map.put(3,b3); 

      //Traversing map 

      for(Map.Entry<Integer, Book>entry:map.entrySet()){   

                int key=entry.getKey(); 

            Book b=entry.getValue(); 

            System.out.println(key+" Details:"); 

            System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  

    }   

  }   

}   

 

 

// Example 39.4: A Map container using EnumMap class

 

/* This Java program illustrates working of EnumMap and its functions. */

 

import java.util.EnumMap;

public class EnuMapExample {

      public enum SIZE    {S, M, L, X };    

      public static void main(String args[])     {    

      // Creating EnumMap in java with key   as enum type STATE

      EnumMap<SIZE, String>enuMap = new EnumMap<SIZE, String>(SIZE.class);

      //Putting values inside EnumMap in Java

      // Inserting Enum keys different from  their natural order

      enuMap.put(SIZE.S, "Children");

      enuMap.put(SIZE.M, "Young");

      enuMap.put(SIZE.L, "Aged");

      enuMap.put(SIZE.X, "Old");

      // Printing size of EnumMap in java

      System.out.println("Size of EnumMap in java: "+  enuMap.size());

      // Printing Java EnumMap 

      System.out.println("EnumMap: "+ enuMap);

      // Retrieving value from EnumMap in java

      System.out.println("Key : "+ SIZE.S +" Value: " +

      enuMap.get(SIZE.S));

     }

}

 

// Example 39.5: Synchronized (thread-safe) Map

import java.util.*;

class HashMapBulkOperationDemo1 {

     public static void main(String args[]) {

      // Create two map object containers.

      Map<Integer, String> map = Collections.synchronizedMap(new HashMap<>());

      // Creatin a map

      map.put(400, "Bad Request");

      map.put(304, "Not Modified");

      map.put(200, "OK");

      map.put(301, "Moved Permanently");

      map.put(500, "Internal Server Error");

      Set<Integer>keySet = map.keySet();

      synchronized(map) {

          Iterator<Integer> iterator = keySet.iterator();

               while(iterator.hasNext()) {

                    Integer key = iterator.next();

                    String value = map.get(key);

               }

      }

     }

}